/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */

package cheat.entities;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * Represents a user of the system.
 *
 * <p>There are two states a user may be in: ACTIVE, and PENDING (see {@link StatusType}).</p>
 *
 * <p>Initially, a user is PENDING after they self-regisiter.</p>
 *
 * <p>An admin user then either grants or denies the registration.  Denied users are
 * deleted from the system.  Approved registrations then become ACTIVE.</p>
 *
 * @author Keith Shie
 */
@Entity
@Table(name = "reguser")
@NamedQueries({@NamedQuery(name = "RegisteredUser.findAll", query = "SELECT r FROM RegisteredUser r"),
        @NamedQuery(name = "RegisteredUser.findByUserId", query = "SELECT r FROM RegisteredUser r WHERE r.userId = :userId"),
        @NamedQuery(name = "RegisteredUser.findByPassword", query = "SELECT r FROM RegisteredUser r WHERE r.password = :password")})
public class RegisteredUser implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @Column(name = "user_id", nullable = false, length = 10, unique=true)
  private String userId;
  @Basic(optional = false)
  @Column(name = "password", nullable = false, length = 30)
  private String password;
  @JoinColumn(name = "accesscd", referencedColumnName = "accesscd", nullable = false)
  @ManyToOne(optional = false, fetch=FetchType.EAGER)
  private AccessType accessCode;
  @JoinColumn(name = "person_id", referencedColumnName = "person_id", nullable = false)
  @OneToOne(optional = false)
  private Person person;
  @JoinColumn(name = "statcd", referencedColumnName = "statcd", nullable = false)
  @ManyToOne(optional = false, fetch=FetchType.EAGER)
  private StatusType statCode;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "ownerId",fetch=FetchType.LAZY)
  private Collection<AssignmentProperty> asgnPropCollection;

  public RegisteredUser() {
  }

  public RegisteredUser(String userId) {
    this.userId = userId;
  }

  public RegisteredUser(String userId, String password) {
    this.userId = userId;
    this.password = password;
  }

  public String getUserId() {
    return userId;
  }

  public void setUserId(String userId) {
    this.userId = userId;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public AccessType getAccessCode() {
    return accessCode;
  }

  public void setAccessCode(AccessType accesscd) {
    this.accessCode = accesscd;
  }

  public Person getPerson() {
    return person;
  }

  public void setPerson(Person person) {
    this.person = person;
  }

  public StatusType getStatCode() {
    return statCode;
  }

  public void setStatCode(StatusType statCode) {
    this.statCode = statCode;
  }

  public Collection<AssignmentProperty> getAsgnPropCollection() {
    return asgnPropCollection;
  }

  public void setAsgnPropCollection(Collection<AssignmentProperty> asgnPropCollection) {
    this.asgnPropCollection = asgnPropCollection;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (userId != null ? userId.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof RegisteredUser)) {
      return false;
    }
    RegisteredUser other = (RegisteredUser) object;
    if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "cheat.entities.Reguser[userId=" + userId + "]";
  }

}
